package com.core.cloundbusinessapi.system.log.dto;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @author =====Lqj=====
 * @className LoginLogDTO
 * @description 登录日志实体
 * @create 2024/12/16 12:00
 **/
@Getter
@Setter
@TableName("sys_login_log")
public class LoginLogDTO {

    /**
     * 主键ID
     */
    @TableId
    private Long id;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 真实姓名
     */
    private String realName;

    /**
     * 登录IP
     */
    private String loginIp;

    /**
     * 登录地址
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 设备类型
     */
    private String deviceType;

    /**
     * 设备ID
     */
    private String deviceId;

    /**
     * 设备名称
     */
    private String deviceName;

    /**
     * 登录类型：0-账号密码，1-手机验证码
     */
    private Integer loginType;

    /**
     * 登录结果：0-失败，1-成功
     */
    private Integer loginResult;

    /**
     * 失败原因
     */
    private String failReason;

    /**
     * 登录时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginTime;

    /**
     * 退出时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date logoutTime;

    /**
     * 会话时长（分钟）
     */
    private Long sessionDuration;

    /**
     * Token
     */
    private String token;

    /**
     * 用户代理
     */
    private String userAgent;

    /**
     * 请求来源
     */
    private String referer;

    /**
     * 备注
     */
    private String remark;

    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 更新时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 删除标志：0-正常，1-删除
     */
    private Integer delFlag;

    /**
     * 构造函数
     */
    public LoginLogDTO() {
        this.createTime = new Date();
        this.updateTime = new Date();
        this.delFlag = 0;
    }

    /**
     * 便捷构造函数
     */
    public LoginLogDTO(Long userId, String userName, String realName, String loginIp, 
                      Integer loginType, Integer loginResult, String failReason) {
        this();
        this.userId = userId;
        this.userName = userName;
        this.realName = realName;
        this.loginIp = loginIp;
        this.loginType = loginType;
        this.loginResult = loginResult;
        this.failReason = failReason;
        this.loginTime = new Date();
    }

    /**
     * 设置登录成功
     */
    public void setLoginSuccess(String token) {
        this.loginResult = 1;
        this.token = token;
        this.failReason = null;
    }

    /**
     * 设置登录失败
     */
    public void setLoginFail(String reason) {
        this.loginResult = 0;
        this.failReason = reason;
        this.token = null;
    }

    /**
     * 设置退出信息
     */
    public void setLogoutInfo() {
        this.logoutTime = new Date();
        if (this.loginTime != null) {
            long duration = (this.logoutTime.getTime() - this.loginTime.getTime()) / (1000 * 60);
            this.sessionDuration = duration;
        }
    }

    /**
     * 设置设备信息
     */
    public void setDeviceInfo(String deviceId, String deviceName, String deviceType) {
        this.deviceId = deviceId;
        this.deviceName = deviceName;
        this.deviceType = deviceType;
    }

    /**
     * 设置浏览器和操作系统信息
     */
    public void setBrowserAndOs(String userAgent) {
        this.userAgent = userAgent;
        if (userAgent != null) {
            // 简单的浏览器检测
            if (userAgent.contains("Chrome")) {
                this.browser = "Chrome";
            } else if (userAgent.contains("Firefox")) {
                this.browser = "Firefox";
            } else if (userAgent.contains("Safari")) {
                this.browser = "Safari";
            } else if (userAgent.contains("Edge")) {
                this.browser = "Edge";
            } else {
                this.browser = "Unknown";
            }

            // 简单的操作系统检测
            if (userAgent.contains("Windows")) {
                this.os = "Windows";
            } else if (userAgent.contains("Mac")) {
                this.os = "macOS";
            } else if (userAgent.contains("Linux")) {
                this.os = "Linux";
            } else if (userAgent.contains("Android")) {
                this.os = "Android";
            } else if (userAgent.contains("iOS")) {
                this.os = "iOS";
            } else {
                this.os = "Unknown";
            }
        }
    }
}
